package com.xd.core.system.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.xd.common.constant.SysConstant;
import com.xd.common.util.SecurityUtil;
import com.xd.core.system.dto.*;
import com.xd.core.system.entity.GroupInfo;
import com.xd.core.system.entity.SysRole;
import com.xd.core.system.entity.SysUser;
import com.xd.core.system.service.IGroupInfoService;
import com.xd.core.system.service.ISysUserService;
import com.xd.core.system.vo.PersonalInfoVo;
import com.xd.core.system.vo.SysUserNameVo;
import com.xd.core.system.vo.UserDetailVo;
import com.xd.core.system.vo.UserListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author SJ
 * @since 2023-03-14
 */
@Api(tags = "用户信息")
@Slf4j
@RestController
@RequestMapping("/sys-user")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IGroupInfoService groupInfoService;


    @PreAuthorize("hasAnyAuthority('sys:AddMployee')")
    @ApiOperation("新增员工")
    @PostMapping("/addUser")
    public void addUser(@RequestBody AddSysUserDto addSysUserDto) {
        sysUserService.addUser(addSysUserDto);
        log.info("新增员工成功");
    }

    @ApiOperation("新增员工-角色下拉框")
    @GetMapping("/roleDropDown")
    public List<SysRole> roleListDropDown() {
        return sysUserService.roleListDropDown();
    }

    @ApiOperation("新增员工-部门下拉框")
    @GetMapping("/groupDropDown")
    public List<GroupInfo> groupDropDown() {
        return groupInfoService.groupDropDown();
    }

    @ApiOperation("新增员工-数据权限下拉框")
    @GetMapping("/dataPerDropDown")
    public List<GroupInfo> dataPerDropDown() {
        return groupInfoService.dataPerDropDown();
    }


    @PreAuthorize("hasAnyAuthority('sys:EmployeeManagement','sys:SystemAdministration')")
    @ApiOperation("员工列表")
    @PostMapping("/userList")
    public Page<UserListVo> userList(@RequestBody UserListDto userListDto) {
       return sysUserService.userList(userListDto);
    }
    @ApiOperation("员工详情")
    @GetMapping("/userDetail")
    public UserDetailVo userDetail(@RequestParam(required = false) Integer id) {
        if (id == null)
            id = SecurityUtil.getLoginUserId();
        return sysUserService.userDetail(id);
    }


    @PreAuthorize("hasAnyAuthority('sys:EmployeeManagement-UpdateMployee')")
    @ApiOperation("修改员工")
    @PostMapping("/updateUser")
    public void updateUser(@RequestBody UpdateSysUserDto updateSysUserDto) {
        sysUserService.updateUser(updateSysUserDto);
        log.info("修改员工成功");
    }


    @PreAuthorize("hasAnyAuthority('sys:EmployeeManagement-Delete')")
    @ApiOperation("删除员工")
    @PostMapping("/deleteUser")
    public void deleteUser(Integer id) {
        sysUserService.deleteUser(id);
    }

    @ApiOperation("账号启停")
    @PostMapping("/updateUserStatus")
    public void updateUserStatus(@RequestBody UpdateStatusDto updateStatusDto) {
        SysUser sysUser = new SysUser();
        sysUser.setId(updateStatusDto.getId());
        sysUser.setStatus(updateStatusDto.getStatus());
        sysUserService.updateById(sysUser);
    }

    @PreAuthorize("hasAnyAuthority('sys:AssetManagement-ChangePassword','sys:AgentManagement-ChangePassword','sys:GroupCompanyPassword')")
    @ApiOperation("平台修改资方--代理登录密码")
    @PostMapping("/updatePassword")
    public void updatePassword(@RequestBody UpdatePasswordDto dto){
        sysUserService.updatePassword(dto);
    }

    @PreAuthorize("hasAnyAuthority('sys:PersonalCenter')")
    @ApiOperation("个人资料")
    @GetMapping("/personalInfo/{userId}")
    public PersonalInfoVo getPersonalInfo(@PathVariable Integer userId) {
        return sysUserService.getPersonalInfo(userId);
    }

    @ApiOperation("资方员工下拉框")
    @GetMapping("/capitalUsers/{companyId}")
    public List<SysUserNameVo> capitalUsers(@PathVariable Integer companyId) {
        return BeanUtil.copyToList(sysUserService.list(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getCompanyId, companyId)
                .eq(SysUser::getDelFlag, SysConstant.XKD_DEL_FLAG_NOT_DELETED)
                .eq(SysUser::getStatus,SysConstant.USER_ENABLE)), SysUserNameVo.class);
    }

}
